﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SIMS.School;
using SIMS.Global;
using SIMS.Company;

public partial class School_Room_CreateEdit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            foreach (RoomType suit in Enum.GetValues(typeof(RoomType)))
            {
                ddlTypeOfRoom.Items.Add(new ListItem(suit.ToString(), ((int)suit).ToString()));
            }

            if (Session[Global.CompanyId] != null)
            {
                hidCompanyId.Value = Session[Global.CompanyId].ToString();
                lblCompany.Text = Session[Global.CompanyName].ToString();
                if (Request.Params["id"] != null)
                {
                    int _RoomId = 0;
                    if (int.TryParse(Request.Params["id"].ToString(), out _RoomId) && _RoomId > 0)
                    {
                        Room _Room = new Room();
                        RoomDB _RoomDB = new RoomDB();
                        _Room = _RoomDB.GetRoom(_RoomId, Convert.ToInt32(hidCompanyId.Value));

                        if (_Room.Id > 0)
                        {
                            if (_Room.Error.ErrorNo > 0)
                            {
                                lblMessage.Text = _Room.Error.ErrorMessage;
                            }
                            else
                            {
                                hidRoomId.Value = _RoomId.ToString();
                                txtRoomName.Text = _Room.Name;
                                txtSeatNo.Text = _Room.SeatNo.ToString();
                                txtDescription.Text = _Room.Description;
                                ddlTypeOfRoom.Text = ((int)(_Room.TypeOfRoom)).ToString();
                            }
                        }
                    }
                    else
                    {
                        lblMessage.Text = "Invalid parameter!";
                    }
                }
            }
            else
            {
                lblMessage.Text = "No company found!";
                btnSubmit.Enabled = false;
            }
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            if (Page.IsValid)
            {
                Room Room = new Room();
                RoomDB _RoomDB = new RoomDB();
                int _RoomId = 0;
                int _CompanyId = 0;

                if (int.TryParse(hidRoomId.Value, out _RoomId) && _RoomId > 0)
                {
                    Room = _RoomDB.GetRoom(_RoomId, Convert.ToInt32(hidCompanyId.Value));
                }
                else
                {
                    Room.Id = 0;
                    Room.CompanyId = Convert.ToInt32(hidCompanyId.Value);
                    Room.Error = new MyError();
                }

                if (int.TryParse(hidCompanyId.Value, out _CompanyId) && _CompanyId > 0)
                {
                    Room.CompanyId = _CompanyId;
                }
                else
                {
                    lblMessage.Text = "Invalid company id.";
                    return;
                }

                Room.Name = txtRoomName.Text;

                Room.TypeOfRoom = (RoomType)Enum.Parse(typeof(RoomType), ddlTypeOfRoom.Text);

                if (txtSeatNo.Text.Trim().Length > 0)
                {
                    Int16 _seatNo = 0;
                    if (Int16.TryParse(txtSeatNo.Text, out _seatNo))
                    {
                        Room.SeatNo = _seatNo;
                    }
                    else
                    {
                        Room.SeatNo = 0;
                    }
                }
                else
                {
                    Room.SeatNo = 0;
                }

                if (txtDescription.Text.Trim().Length > 0)
                {
                    Room.Description = txtDescription.Text;
                }
                else
                {
                    Room.Description = string.Empty;
                }

                Room.IsActive = false;

                _RoomDB.Save(Room);

                if (Room.Error.ErrorNo > 0)
                {
                    if (Room.Error.ErrorNo == 1)
                    {
                        lblMessage.Text = Room.Error.ErrorMessage;
                    }
                    else
                    {
                        lblMessage.Text = "(" + Room.Error.ErrorLocation + ")";
                        lblMessage.Text += Room.Error.ErrorMessage;
                        lblMessage.Text += "<br/><br/>" + Room.Error.ErrorStack;
                    }
                }
                else
                {
                    lblMessage.Text = "Saved successfully!";
                    Response.Redirect("default.aspx?companyid=" + hidCompanyId.Value);
                }
            }
            else
            {
                lblMessage.Text = "Invalid form.";
            }
        }
        catch (Exception Ex)
        {
            lblMessage.Text = Ex.Message;
            MyError.CreateExceptionLog(Ex, "Company_CreateEdit");
        }

    }

}